package com.supconit.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.supconit.entity.Share;
import com.supconit.entity.View;
import com.supconit.entity.Vote;
import com.supconit.service.*;
import com.supconit.util.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * <p>
    *  前端控制器
    * </p>
 *
 * @author xjh
 * @since 2019-08-17
 */
@RestController
@RequestMapping("/vote")
@Api(description = "")
public class VoteController  extends  BaseController{

    @Autowired
    private IVoteService voteService;
    @Autowired
    private IViewService viewService;
    @Autowired
    private IRankViewService rankService;
    @Autowired
    private ITotalViewService totalService;

    @Autowired
    private IShareService shareService;

    private static final Logger logger = LoggerFactory.getLogger(VoteController.class);

    @Value("${wechat.vote_num_max}")
    private int vote_num_max;

    @Autowired
    RedisUtils redisUtils;
//    /**
//     * 列表
//     */
//    @PostMapping(value = "/list")
//    @ApiOperation(value = "列表", notes = "列表")
//    public Object list() {
//        QueryWrapper<Vote> wrapper = new QueryWrapper<>();
//        return voteService.list(wrapper);
//    }
//
//     /**
//     * 分页
//     */
//    @PostMapping(value = "/page")
//    @ApiOperation(value = "分页", notes = "分页")
//    public Object page(Page<Vote> page) {
//        QueryWrapper<Vote> wrapper = new QueryWrapper<>();
//        return voteService.page(page, wrapper);
//    }

    /**
     * 详情
     */
    @GetMapping(value = "/getTotal")
    @ApiOperation(value = "获取总数", notes = "获取总数")
    public Object getTotal() {
        return  redisUtils.get("vote_total");
    }

    /**
     * 详情
     */
    @GetMapping(value = "/getRank")
    @ApiOperation(value = "获取排行", notes = "获取排行")
    public Object getRank() {
        return  redisUtils.get("vote_rank");
//        return rankService.list();
    }


    /**
     * 详情
     */
    @GetMapping(value = "/getById/{id}")
    @ApiOperation(value = "详情", notes = "详情")
    public Object getById(@PathVariable("id") Long id) {
        return voteService.getById(id);
    }

     /**
     * 新增
     */
    @PostMapping(value = "/save")
    @ApiOperation(value = "新增", notes = "新增")
    public Object save(Vote vote) {
        vote.setVoteTime(LocalDateTime.now());
        logger.info("投票人："+vote.getVotePerson());
        if(StringUtils.isEmpty(vote.getVotePerson()) || StringUtils.isEmpty(vote.getVoteContent())){
            return false;
        }
        QueryWrapper<View> wrapper = new QueryWrapper<View>();
        wrapper.lambda().eq(View::getVotePerson,vote.getVotePerson());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMdd");
        wrapper.lambda().eq(View::getVoteDate, formatter.format(vote.getVoteTime()));
        List<View> view = viewService.list(wrapper);
        // 判断是否有分享
        QueryWrapper<Share> wrapper2 = new QueryWrapper<Share>();
        wrapper2.lambda().eq(Share::getVotePerson,vote.getVotePerson());
        wrapper2.lambda().eq(Share::getShareDate, formatter.format(vote.getVoteTime()));
        List<Share> view2 =  shareService.list(wrapper2);
        int max = vote_num_max;
        // 今日有分享可以多投一次
        if(view2!=null && view2.size()>0){
            max=max+1;
        }
        // 不能重复投票
        if(view!=null && view.size()>max){
            return false;
        }
        vote.setId(null);
        return voteService.save(vote);
    }

        /**
     * 分享
     */
    @PostMapping(value = "/share")
    @ApiOperation(value = "分享", notes = "分享")
    public Object share(String votePerson) {
        Share share = new Share();
        share.setVotePerson(votePerson);
        SimpleDateFormat formatter =new SimpleDateFormat("yyMMdd");
        share.setShareDate(formatter.format(new Date()));
        return shareService.save(share);
    }

//    /**
//     * 删除
//     */
//    @PostMapping(value = "/removeById")
//    @ApiOperation(value = "删除", notes = "删除")
//    public Object removeById(@RequestParam Long id) {
//        return voteService.removeById(id);
//    }
//
//    /**
//     * 修改
//     */
//    @PostMapping(value = "/updateById")
//    @ApiOperation(value = "修改", notes = "修改")
//    public Object updateById(Vote vote) {
//        return voteService.updateById(vote);
//    }
}
